package com.example.unisys.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.unisys.entity.Course;
import com.example.unisys.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/apis/v1/courses", produces = "application/json; charset=utf-8")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /*
    * 插入仓库课程
    * */
//    @RequestMapping("insert")
//    public int insert(@RequestBody JSONObject jsonParam) {
//        int returncode = 0;
//        Course course = JSON.toJavaObject(jsonParam, Course.class);
//        returncode = courseService.save(course);
//        return returncode;
//    }

    @RequestMapping("update")
    public int update(@RequestBody JSONObject jsonParam) {
        int returncode = 0;
//        System.out.println(jsonParam.toString());
        Course course = JSON.toJavaObject(jsonParam, Course.class);
        returncode = courseService.update(course);
        return returncode;
    }

    @RequestMapping("getById/{course_id}")
    public Course getById(@PathVariable("course_id") String course_id) {
        return courseService.getById(course_id);
    }

//    @RequestMapping("getCourseSum")
//    public int getCourseSum() {
//        return courseService.getCourseSum();
//    }

    @RequestMapping(method = {RequestMethod.POST})
    public HashMap<String, Object> getAllCourse(@RequestBody JSONObject jsonParam) {
        //HashMap<String, Object> hashMap=JSON.parseObject(jsonParam,  new TypeReference<Map<String, Student>>);

        HashMap<String, Object> params = JSONObject.parseObject(jsonParam.toJSONString(), new TypeReference<HashMap<String, Object>>() {
        });
        int pagesize = (Integer) params.get("limit");
        int start = ((Integer) params.get("page") - 1) * pagesize;
        List<Course> res = courseService.getAllCourse(start, pagesize);
        HashMap<String, Object> responsedata = new HashMap();
        responsedata.put("code", 20000);
        responsedata.put("data", res);
        return responsedata;
    }


}
